package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.ParseException
import java.text.SimpleDateFormat;
import java.time.Instant

import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import cn.com.kh.hst.ThreeDesUtil
import cn.com.kh.KHUtil;
import cn.com.kh.getProperty;


/**
 *
 * @Title: aipHSTOldCustUpdate
 * @Description:华盛证券 客户服务顾问数据 后台更新接口 --内部接口
 * @author: chieh
 * @date 2021-09-25 16:00:00
 *
 */

public class aipHSTOldCustUpdate implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(aipHSTOldCustUpdate.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);

        String inData = "";
        String outData = "";
        String totalData = "";
        String dataSourceID = "dataSource";


        BufferedReader iny = null;
        if (getMethod.equals("GET")) {
            inData = request.getParameter("data");
            log.debug("原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("原始Post请求参数:" + inData);
        }

        inData = URLDecoder.decode(inData, "UTF-8");
        log.debug("原始inData URLDecoder:" + inData);

        try {

            long startTime = System.currentTimeMillis();
            String reJson = null;
            long endTime = 0;
			JSONObject inObject = new JSONObject(inData);
            

			def selectime=eliteBean.chaxun_sql("select oc.customer_guid as customer_guid,o.OBJECTIVE_GUID,o.HANDLEBY_ID,o.HANDLEGROUP_ID,oc.accountExecutiveId from  objective o left join oldcustomer114 oc  on oc.customer_guid=o.customer_guid where oc.customer_guid!='' and o.HANDLEBY_ID is null and o.HANDLEGROUP_ID is null","dataSource");
			log.debug("长度："+selectime.size());
			
			for (int i = 0; i < selectime.size(); i++) {
				def customer_guid = selectime.get(i).get("customer_guid");
				
				 String[] custTaskDealSQL = new String[2];
					// 更新OBJECTIVE表
					custTaskDealSQL[0] = "update objective o set  o.HANDLEBY_ID='000184',o.HANDLEGROUP_ID='TEAM01' where o.customer_guid='"+customer_guid+"'";

					// 插入elitetask表
					custTaskDealSQL[1] = "update elitetask o set  o.HANDLEBY_ID='000184',o.HANDLEGROUP_ID='TEAM01' where o.customer_guid='"+customer_guid+"'";					
							
					int[] batchSQLResult = eliteBean.updateBatch(custTaskDealSQL, "dataSource");
					log.debug("客户归属人人批量处理SQL返回结果："+batchSQLResult.toString());
			}
           
            outData = "{\"result\":\"1\",\"message\":\"处理成功\",\"data\":[]}";


        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            outData = "{\"result\":\"-1\",\"message\":\"处理失败(" + e.getMessage() + ")\",\"data\":[]}";
        }

        return outData;
    }


    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        log.debug("解析-k=" + getKey + " v=" + Rev_Value);
        return Rev_Value;
    }


    // 非空校验isNull
    public static boolean isNull(String value) {

        boolean reFlag = true;
        if (value == "" || value.length() <= 0) {
            reFlag = true;
        } else {
            reFlag = false;
        }
        log.debug(reFlag);
        return reFlag;
    }

    // 非空校验isNullMSG
    public static String isNullMSG(String key, String value) {

        String reResult = "";
        if (value == "" || value.length() <= 0) {
            reResult = key + "必填字段;";
        }
        return reResult;
    }

}
